﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Reflection;
using System.Threading;

namespace intelliEssay.Graphics.Shell
{
    /// <summary>
    /// Interaction logic for Load.xaml
    /// </summary>
    public partial class Load : Window
    {
        public Load()
        {
            InitializeComponent();
            this.Loaded += Load_Loaded;
        }

        void Load_Loaded(object sender, RoutedEventArgs e)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(o =>
                {
                    var dictSuffix = Graphics.Shell.GraphicsSetting.Default.DisplayCulture.ToUpperInvariant();
                    var currentDictionary = Directory.GetCurrentDirectory() + "\\langpacks\\intelliEssay.Graphics.Shell." + dictSuffix + ".dll";
                    Assembly assembly = Assembly.LoadFile(currentDictionary);
                    var shellResourceDictionary = assembly.GetExportedTypes().FirstOrDefault<Type>(type => type.Name == "ShellResourceDictionray");
                    var dictionaryInstance = Activator.CreateInstance(shellResourceDictionary);
                    var resourceDict = new intelliEssay.Graphics.Globalization.ResourceDictionary(dictionaryInstance);
                    resourceDict.Load();
                    GraphicsSetting.Default.ResourceDictionary = resourceDict;
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        transitioningContentControl.Content = resourceDict["startup"];
                    }));
                    Thread.Sleep(500);
                    var files = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\langpacks\\").Where<string>(filename =>
                        {
                            return filename.Contains("Shell." + dictSuffix + ".dll") == false && filename.EndsWith(".dll");
                        }).ToArray();
                    Environment.SupportedLanguages = new string[files.Count() + 1];
                    Environment.SupportedLanguageIDs = new string[files.Count() + 1];
                    Environment.SupportedLanguages[0] = resourceDict["currentlanguage"];
                    Environment.SupportedLanguageIDs[0] = resourceDict["LanguageID"];
                    for (int i = 0; i < files.Count(); i++)
                    {
                        try
                        {
                            this.Dispatcher.Invoke(new Action(() =>
                            {
                                transitioningContentControl.Content = string.Format("{0} {1}%", resourceDict["loadingUI"], ((i+1) * 100) / files.Count());
                            }));
                            Thread.Sleep(100);
                            Assembly _assembly = Assembly.LoadFile(files[i]);
                            var _shellResourceDictionary = _assembly.GetExportedTypes().FirstOrDefault<Type>(type => type.Name == "ShellResourceDictionray");
                            dynamic _dictionaryInstance = Activator.CreateInstance(_shellResourceDictionary);
                            Environment.SupportedLanguages[i + 1] = _dictionaryInstance.CurrentLanguage;
                            Environment.SupportedLanguageIDs[i + 1] = _dictionaryInstance.LanguageID;
                        }
                        catch
                        {
                            continue;
                        }
                    
                    }
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        transitioningContentControl.Content = string.Format("{0} {1}%", resourceDict["loadingcomponents"],"0");
                    }));
                    var registeredComponents = intelliEssay.Core.Platform.FeatureProbing.GetRegisteredComponents();
                    for (int i = 0; i < registeredComponents.Count; i++)
                    {
                        try
                        {
                            registeredComponents[i].Load(Graphics.Shell.GraphicsSetting.Default.DisplayCulture);
                        }
                        catch
                        {
                            continue;
                        }
                        this.Dispatcher.Invoke(new Action(() =>
                        {
                            transitioningContentControl.Content = string.Format("{0} {1}%", resourceDict["loadingcomponents"], (((i+1) * 100) / registeredComponents.Count));
                        }));
                        Thread.Sleep(100);
                    }
                    Environment.LoadedComponents = new Core.Platform.ComponentCollection(registeredComponents.Where<Core.Platform.Component>(comp => comp.IsLoaded));
                    this.Dispatcher.Invoke(new Action(() =>
                        {
                            MainWindow.Create().Show();
                            this.Close();
                        }));
                });
        }
    }
}
